home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap7 / 7_2 / ht72a.c next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  3.7 KB  |  143 lines

  1. /*
  2.  * Compile with: 
  3.  *   cc string.o array.o dict.o cgilib.o \
  4.  *       ht72a.c -o ht72a.cgi -lgd -lm
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. #include <gd.h>
  10. #include <gdfontmb.h> /* support for the Medium-Bold font */
  11. #include <gdfonts.h>  /* support for the small font       */
  12.  
  13. #include "cgilib.h"
  14.  
  15.  
  16. void centerString(
  17.     gdImagePtr img,  /* gd Image to render in                  */
  18.     gdFontPtr font,  /* gd Font to render with                 */
  19.     int x, int y,    /* x and y coordinates of center of string*/
  20.     char *txt,       /* text to render                         */
  21.     int colour)      /* colour to render the text in           */
  22. {
  23.     /* Calculate the width of the text in pixels */
  24.     int width = strlen(txt)*(font->w);
  25.  
  26.     gdImageString(img,font,1+x-width/2,y,txt,colour);
  27. }
  28.  
  29.  
  30. int main(void)
  31. {
  32.     Dictionary dataDict = readParse();
  33.     int  max, i, x, barLen;
  34.     char buf[80];
  35.  
  36.     int barWidth = dict_isKey(dataDict,"barWidth")
  37.         ? atoi(dict_valueForKey(dataDict,"barWidth"))
  38.         : 20;
  39.  
  40.     int barHeight = dict_isKey(dataDict,"barHeight")
  41.         ? atoi(dict_valueForKey(dataDict,"barHeight"))
  42.         : 100;
  43.    
  44.     int barPadding = dict_isKey(dataDict,"barPadding")
  45.         ? atoi(dict_valueForKey(dataDict,"barPadding"))
  46.         : 2;
  47.        
  48.     int score[10] = {1,2,3,4,5,6,7,8,9,10};
  49.  
  50.     int topPadding    = 15;
  51.     int bottomPadding = 20;
  52.     int xAxis         = topPadding+barHeight;
  53.     int width         = 10*barWidth+11*barPadding;
  54.     int height        = barHeight+topPadding+bottomPadding;
  55.  
  56.     /* Create a blank gd image of the required size */
  57.     gdImagePtr im     = gdImageCreate(width, height);
  58.  
  59.     /* Allocate colors */
  60.     int black         = gdImageColorAllocate(im,  0,  0,  0);
  61.     int white         = gdImageColorAllocate(im,255,255,255);
  62.     int blue          = gdImageColorAllocate(im,  0,  0,255);
  63.  
  64.     /*
  65.      * Overide the defaults for score[] if the CGI
  66.      * variable 'score' was specified
  67.      */
  68.     if (dict_isKey(dataDict,"score"))
  69.         sscanf(dict_valueForKey(dataDict,"score"),
  70.            "%d %d %d %d %d %d %d %d %d %d",
  71.            &score[0], &score[1], &score[2], &score[3],
  72.            &score[4], &score[5], &score[6], &score[7],
  73.            &score[8], &score[9]); 
  74.        
  75.     /* Make the image interlaced */
  76.     gdImageInterlace(im, 1);
  77.  
  78.     /* Output the GIF content type */
  79.     printf("Content-type: image/gif\n\n"); 
  80.  
  81.     /* Draw the X-axis */
  82.     gdImageLine(im, 0, xAxis, width-1, xAxis, white);    
  83.  
  84.     /* Determine the height of the highest bar */
  85.     for ( max=0, i=0 ; i<10 ; i++ )
  86.     {
  87.         if (score[i] > max)
  88.     {
  89.         max = score[i];
  90.     }
  91.        
  92.     }
  93.    
  94.     for ( i=0 ; i<10 ; i++ )
  95.     {
  96.         /* Compute the X coordinate for the center of the bar */
  97.         x = i*barWidth+barWidth/2+(i+1)*barPadding;
  98.  
  99.         /* Calculate how high this bar should be */
  100.         barLen = score[i]*barHeight/max;
  101.       
  102.         /* Draw a tick mark on the x-axis */
  103.         gdImageLine(im, x, xAxis+1, x, xAxis+2, white);
  104.  
  105.         /*
  106.      * Label the x-axis with a number from 1 to 10
  107.      * using the bold font.
  108.      */
  109.         sprintf(buf,"%d",i+1);
  110.         centerString(im,
  111.              gdFontMediumBold,
  112.              x,xAxis+3,      /* X, Y coordinates */
  113.              buf,            /* text to display  */
  114.              white);
  115.  
  116.         /* label the height of the bar using the small font */
  117.         sprintf(buf,"%d",score[i]);
  118.         centerString(im,
  119.              gdFontSmall,
  120.              x,xAxis-barLen-11,
  121.              buf,
  122.              white);
  123.  
  124.         /* draw one bar */
  125.         gdImageFilledRectangle(
  126.         im, 
  127.         x-barWidth/2, xAxis-barLen,   
  128.         x+barWidth/2, xAxis-1,         
  129.         blue);
  130.     }
  131.  
  132.     /*
  133.      * Convert the image to GIF and print it to
  134.      * standard output
  135.      */
  136.     gdImageGif(im, stdout);
  137.  
  138.     /* Destroy image */
  139.     gdImageDestroy(im);
  140.  
  141.     return 0;
  142. }
  143.